package chapter04

import org.apache.log4j.{Level, Logger}
import org.apache.spark.sql.{SaveMode, SparkSession}

object Test15_MySQLToHive {
  def main(args: Array[String]): Unit = {
    System.setProperty("HADOOP_USER_NAME","root")
    Logger.getLogger("org.apache.spark").setLevel(Level.WARN)
    val spark = SparkSession.builder()
      .appName("toHive")
      .master("local[*]")
      .enableHiveSupport()
      .getOrCreate()
    import spark.implicits._
    //读取MySQL中数据
    val url = "jdbc:mysql://hadoop01:3306/student?" +
      "useUnicode=true&characterEncoding=utf8&useSSL=false"
    val frame = spark.read
      .format("jdbc")
      .option("url", url)
      .option("user", "root")
      .option("password", "123456")
      .option("dbtable", "salary")
      .load()
    frame.show(5)
    frame.printSchema()
    frame.withColumn("Age",$"Age".cast("String"))
      .printSchema()
    //写入hive
    frame.write
      .mode(SaveMode.Append)
      .saveAsTable("db_hive1.salary2")
  }
}
